<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\bootstrap\ActiveForm;
use yii\widgets\LinkPager;
use yii\grid\GridView;
use yii\helpers\Url;
$this->title = '权限管理';
?>
<div class="container">
	<div class="row">
		<?php echo $this->render('_leftnav') ?>
		<div class="col-md-10">
			<div class="page-header clearfix">
				<h1 class="pull-left">权限管理</h1>
			</div>
			<?php echo $this->render('_adminnav') ?>
			<?php 
				$form = ActiveForm::begin([
					'options' => ['class' => 'form-inline well well-sm',
					'id'=>'comment-search-form'],
					'action'=>Url::to(['user/auth']),
					'method'=>'post',
					'fieldConfig' => [
						'template' => '<div class="form-group"><div class="col-md-8 controls"  style="padding-top: 9px">{input}{error}</div></div>'
					], 
				]);
			?>
				<div class="form-group">
					<?php 
						$auth=['' => '--关键字类型--','node_name' => '节点名称','node_title' => '名称描述'];
					?>
					<?= Html::dropDownList('auth', $search['auth'], ArrayHelper::map($auth,'id', 'auth'), ['class' => 'form-control auth']);?>
				</div>

				<div class="form-group">
					<?= Html::input('text', 'auth_value', $search['value'], ['class' => 'form-control','id' => 'auth_value','placeholder' => '关键字']) ?>
				</div>

				<?= Html::submitButton('搜索', ['class' => 'btn btn-primary','id'=>'auth-search-btn']) ?>
			<?php ActiveForm::end(); ?> 
			<table id="user-table" class="table table-striped table-hover" data-search-form="#user-search-form">
				<thead>
					<tr>
						<th>名称</th>
						<th>名称描述</th>
						<th>状态</th>
						<th>排序</th>
						<th>备用信息</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<?php if($data){?>
					<?php foreach($data as $key => $val){?>
						<tr id="user-table-tr-14">
							<td>
								<strong>
									<?= Html::encode($val['node_name']) ?>
								</strong>
							</td>
							<td>
								<?php if($val['node_title']){
									echo Html::encode($val['node_title']);
								}else{
									echo "--";	
								}?>
							</td>
							<td>
								<?php if($val['node_status'] == "1"){?>
									<span id="<?php echo Html::encode($val['node_id']) ?>"><label class="label label-primary">开启</label></span>
								<?php }else{?>
									<span id="<?php echo Html::encode($val['node_id']) ?>"><label class="label label-danger">禁用</label></span>
								<?php }?>
							</td>
							<td>
								<?php if($val['node_sort']){
									echo Html::encode($val['node_sort']);
								}else{
									echo "--";	
								}?>
							</td>
							<td>
								<?php if($val['node_remark']){
									echo Html::encode($val['node_remark']);
								}else{
									echo "--";
								}?>
							</td>
							<td>
								<div class="btn-group">
									<!-- <a href="javascript:;"  class="btn btn-default btn-sm AdminInfo" data-toggle="modal" data-target="#AdminInfo" type="<?= Html::encode($val['node_id'])?>">查看</a> -->
									<?php if($val['node_status'] == "1"){
									?>
										<a href="javascript:;"  class="btn btn-default btn-sm Off" type="<?= Html::encode($val['node_id'])?>">禁用</a>
									<?php }else{ ?>
										<a href="javascript:;"  class="btn btn-default btn-sm On" type="<?= Html::encode($val['node_id'])?>">启用</a>
									<?php }?>
									
								</div>
							</td>
						</tr>
					<?php }}else{?>
						<tr>
							<td colspan="20">
								<div class="empty">暂无管理员记录</div>
							</td>
						</tr>
					<?php }?>
				</tbody>
			</table>
			<div align="right">
				<?php echo LinkPager::widget([
					'pagination' => $pagination,
					'prevPageLabel'=>'上一页',
					'nextPageLabel'=>'下一页',
				]);?>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	//点击禁用
	$(document).on("click",".Off",function(){
		var id = $(this).attr('type');
		var self = $(this)
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['user/authstatus']);?>",
			data: "id="+id+"&status=0",
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200) {
					self.replaceWith('<a href="javascript:;"  class="btn btn-default btn-sm On" type="'+id+'">启用</a>')
					$("#"+id).html('<label class="label label-danger">禁用</label>')
				}else if(msg.code == 400)
				{
					alert('修改失败')
				}else if(msg.code == 403) 
				{
					alert('暂无权限')
				}
			}
		});
	})
	//点击启用
	$(document).on("click",".On",function(){
		var id = $(this).attr('type');
		var self = $(this)
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['user/authstatus']);?>",
			data: "id="+id+"&status=1",
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200) {
					self.replaceWith('<a href="javascript:;"  class="btn btn-default btn-sm Off" type="'+id+'">禁用</a>')
					$("#"+id).html('<label class="label label-primary">开启</label>')
				}else if(msg.code == 400)
				{
					alert('修改失败')
				}else if(msg.code == 403) 
				{
					alert('暂无权限')
				}
			}
		});
	})
</script>